using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace QuanLySim.DAO
{
    class clsCommand
    {
        private OleDbConnection m_Conn;
        private string m_Erorr;

        public string ERORR
        {
            get { return m_Erorr; }
            set { m_Erorr = value; }
        }

        public clsCommand()
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\\Data\\QLSim.mdb";
            m_Conn = new OleDbConnection();
            m_Conn.ConnectionString = strConn;
            m_Erorr = "";
        }
        public bool Connect()
        {
            try
            {
                m_Conn.Open();
            }
            catch (Exception ex)
            {
                m_Erorr = ex.Message;
                return false;
            }
            return true;
        }

        public void CloseConnect()
        {
            m_Conn.Close();
        }

        public bool ExecuteNonQuery(string strInsert)
        {
            if (m_Conn.State == ConnectionState.Closed)
            {
                if (!this.Connect())
                {
                    return false;
                }
            }
            OleDbCommand command = new OleDbCommand(strInsert, m_Conn);
            if (command.ExecuteNonQuery() <= 0)
            {
                return false;
            }
            return true;
        }

        public DataTable Select(string strSelect)
        {
            DataTable table = new DataTable();
            try
            {
                OleDbCommand Command = new OleDbCommand(strSelect, m_Conn);
                new OleDbDataAdapter(Command).Fill(table);
                Command.Connection.Close();
                return table;
            }
            catch (OleDbException ex)
            {
                m_Erorr = ex.Message;
            }
            return null;
        }
    }
}
